#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){
int shmid, pid;
char* shmaddr_parent, *shmaddr_child;
shmid=shmget((key_t)1234, 10, IPC_CREAT|0644);
if(shmid==-1){
perror("shmget error\n");
exit(1);
}
pid=fork();
if(pid>0){
wait(0);
shmaddr_parent=(char*)shmat(shmid, (char*)NULL, 0);
printf("%s\n", shmaddr_parent);
shmdt((char*)shmaddr_parent);
printf("user place pid addr: %p\n", &pid);
printf("parent print:%p\n", (char*)shmaddr_parent);
printf("parent PID: %d, child(parent) PID: %d\n", getpid(), pid);
}else{
shmaddr_child=(char*)shmat(shmid, (char*)NULL, 0);
strcpy((char*)shmaddr_child, "\nHello Parent!\n");
shmdt((char*)shmaddr_child);
printf("child print:%p\n", (char*)shmaddr_child);
printf("child(child) PID: %d\n", getpid());
exit(0);
}
shmctl(shmid, IPC_RMID, (struct shmid_ds*)NULL);
return 0;
}